﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DbMetadata
{
    /// <summary>
    /// Provides metadata information about a database table.
    /// </summary>
    [Serializable]
    public class DbTableInfo
    {
        private readonly IDictionary<string, DbColumnInfo> _columns =
            new Dictionary<string, DbColumnInfo>();

        private readonly IList<DbRelationInfo> _relations =
            new List<DbRelationInfo>();

        private readonly IList<DbForeignKeyInfo> _foreignKeys =
            new List<DbForeignKeyInfo>();

        /// <summary>
        /// Gets or sets the name of the table.
        /// </summary>
        /// <value></value>
        public string TableName { get; set; }


        /// <summary>
        /// Describes the schema that owns the table.
        /// </summary>
        /// <value></value>
        public string TableSchema { get; set; }

        /// <summary>
        /// Gets or sets the catalog of the table.
        /// </summary>
        /// <value></value>
        public string TableCatalog { get; set; }

        /// <summary>
        /// Gets or sets the <see cref="TableType"/> 
        /// </summary>
        public DbTableType TableType { get; set; }

        /// <summary>
        /// Describes the primary key.
        /// </summary>
        /// <value></value>
        public DbPrimaryKeyInfo PrimaryKey { get; set; }

        /// <summary>
        /// Gets a collection of relations that belongs to this <see cref="DbTableInfo"/>.
        /// </summary>
        /// <value></value>
        /// <remarks>This list will not only include foreign key relations, but also relations from other tables
        /// that has this table as the primary table.</remarks>
        public IList<DbRelationInfo> Relations
        {
            get { return _relations; }
        }

        /// <summary>
        /// Gets a <see cref="IList{T}"/> that contains all foreign key constraints for this <see cref="DbTableInfo"/> instance.
        /// </summary>
        /// <value></value>
        public IList<DbForeignKeyInfo> ForeignKeys
        {
            get { return _foreignKeys; }

        }


        /// <summary>
        /// Gets a collection of columns that belongs to this <see cref="DbTableInfo"/>.
        /// </summary>        
        public IDictionary<string, DbColumnInfo> Columns
        {
            get { return _columns; }
        }

        /// <summary>
        /// Returns a <see cref="System.String"/> that represents this instance.
        /// </summary>
        /// <returns>
        /// A <see cref="System.String"/> that represents this instance.
        /// </returns>
        public override string ToString()
        {
            var sb = new StringBuilder();
            if (!string.IsNullOrEmpty(TableCatalog))
                sb.AppendFormat("{0}.", TableCatalog);
            if (!string.IsNullOrEmpty(TableSchema))
                sb.AppendFormat("{0}.", TableSchema);
            if (!string.IsNullOrEmpty(TableName))
                sb.AppendFormat("{0}", TableName);
            if (sb.Length > 0)
                return sb.ToString();

            return base.ToString();
        }    
    }

}
